﻿Imports System.Data.SqlClient
Public Class AdminForm
    Public Sub remplirCmbCompte()
        Partager.moi.remplir_List_avocat_bureau()
        For Each clt As Avocat_bureau In Partager.moi.List_avocat_bureau
            cmbNom.Items.Add(clt.Nom_a & " " & clt.Prenom_a)
        Next

        Partager.moi.remplir_List_employe()
        For Each clt As Employe In Partager.moi.List_employe
            cmbNom.Items.Add(clt.Nom_a & " " & clt.Prenom_a)
        Next

        If cmbNom.Items.Count > 0 Then
            cmbNom.SelectedIndex = 0
        End If
    End Sub

    Private Sub Btn_ajout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajout.Click
        Try
            Partager.close()
            If (txt_login.Text <> "" And txtPass.Text <> "") Then
                If MsgBox("هل تريد فعلا إضافة هذا المستخدم؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "إضافة") = MsgBoxResult.Yes Then

                    Partager.moi = New Compte(1, txt_login.Text, txtPass.Text, cmbNom.Text, chbPropriet.Checked, txtRem.Text)
                    Partager.Administrat.AjouterCompter(Partager.moi)

                    MsgBox("تمت إضافة المستخدم بنجاح", MsgBoxStyle.Information, "نجاح")
                    Partager.vider(Me)
                End If
            Else
                MsgBox("أدخل البيانات من فضلك", MsgBoxStyle.Critical, "! خـطأ")
            End If
        Catch sqlEx As SqlException
            If sqlEx.Message.Contains("utilisatUnique") Then
                MsgBox("عفوا، اسم المستخدم هذا استعمل من قبل", MsgBoxStyle.Critical, "خطأ")
            Else
                MsgBox(sqlEx.Message)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Btn_modif_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_modif.Click
        Try
            Partager.close()
            If (txt_login.Text <> "" And txtPass.Text <> "") Then
                If MsgBox("هل تريد فعلا تعديل هذا المستخدم؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "تعديل") = MsgBoxResult.Yes Then

                    Partager.moi = New Compte(Partager.moi.Id, txt_login.Text, txtPass.Text, cmbNom.Text, chbPropriet.Checked, txtRem.Text)
                    Partager.Administrat.modifierCompter(Partager.moi)

                    MsgBox("تم تعديل المستخدم بنجاح", MsgBoxStyle.Information, "نجاح")
                    Partager.vider(Me)
                End If
            Else
                MsgBox("أدخل البيانات من فضلك", MsgBoxStyle.Critical, "! خـطأ")
            End If
        Catch sqlEx As SqlException
            If sqlEx.Message.Contains("utilisatUnique") Then
                MsgBox("عفوا، اسم المستخدم هذا استعمل من قبل", MsgBoxStyle.Critical, "خطأ")
            Else
                MsgBox(sqlEx.Message)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Btn_supp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_supp.Click
        If List_tous.SelectedIndices.Count > 0 Then
            If MsgBox("هل تريد فعلا حذف هذا المستخدم ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "حذف") = MsgBoxResult.Yes Then
                Partager.close()
                Try
                    Dim clt As Compte = Partager.Administrat.listCompte(List_tous.SelectedIndices.Item(0))
                    Partager.Administrat.supprimerCompter(clt)
                    List_tous.Items.Clear()
                    remplireListTous()
                    MsgBox("عملية الحذف تمت بنجاح", MsgBoxStyle.Information, "نجاح")
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
        Else
            MsgBox("اختر اجراء من القائمة", MsgBoxStyle.Information, "خطأ")
        End If
    End Sub
    Public Sub remplireListTous()
        For Each khib As Compte In Partager.Administrat.listCompte
            List_tous.Items.Add(New ListViewItem(New String() {khib.user, khib.passe, khib.nom}))
        Next
        Partager.vider(Me)
    End Sub

    Private Sub Btn_nouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_nouv.Click
        Partager.vider(Me)
        txt_login.Select()
    End Sub

    Private Sub AdminForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        remplirCmbCompte()
    End Sub
End Class